﻿namespace Web.Control.WWW.Course
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text.RegularExpressions;
    using System.IO;
    using Lx.Web.Common.ControlsUI;
    using Lx.Web.Common.UI;
    using BLF;
    using System.Collections.Generic;
using Model.WWW_User.Teach;

    public class Course : BaseControl
    {
        /// <summary>
        /// 当前会员编号
        /// </summary>
        protected string MemberNo = "";
        protected long CID;
        /// <summary>
        /// Course信息
        /// </summary>
        protected CoursesModel CourseInfo;
        /// <summary>
        ///(0：当前course更新时间 DateTime、1：此course的老师母语string、2：此course的老师所有session数int、3：此course的教师身份是否有效bool、4浏览者时区stirng)
        /// </summary>
        protected ArrayList ArrCourseInfo;
        /// <summary>
        /// 课件标签
        /// </summary>
        protected List<string> TagList;
        /// <summary>
        /// lesson列表数据
        /// </summary>
        protected List<LessonModel> LessonList;
        protected Repeater rptSessions;
        /// <summary>
        /// course的老师的其他course列表(不包括本course)
        /// </summary>
        protected Repeater rptTeaOthCourse;
        /// <summary>
        /// 与本course所学语言相同的其他course列表
        /// </summary>
        protected Repeater rptOtherCourse;

        protected List<Model.WWW_User.User.UserTimeAvailability> TimeAvaila;
        protected override void OnLoad(EventArgs e)
        {            
            if(string.IsNullOrEmpty(Request["cid"]))
            {
                Response.End();
                return;
            }
            long.TryParse(Request["cid"],out CID);
            MemberNo = HttpContext.Current.User.Identity.Name;
            string sessionID = Session.SessionID;
            string iP = Request.UserHostAddress;
            string userAgent = Request.UserAgent;
            string referer = HttpContext.Current.Request.UrlReferrer == null ? "" : HttpContext.Current.Request.UrlReferrer.AbsoluteUri;
            ArrayList arrList = null;
            long lid = 0;
            if (!string.IsNullOrEmpty(Request["lid"]))
                long.TryParse(Request["lid"], out lid);
            if (lid > 0 && MemberNo!="")
                arrList = BLF.WWW_User.Teach.Teach.GetACourseDelLesson(MemberNo, CID, lid);
            else
                arrList = BLF.WWW_User.Teach.Teach.GetACourse(MemberNo, CID, sessionID, iP, userAgent, referer);
            
            if (arrList != null)
            {
                CourseInfo = arrList[0] as CoursesModel;
                ArrCourseInfo = arrList[1] as ArrayList;
                TagList = arrList[2] as List<string>;
                LessonList = arrList[3] as List<LessonModel>;
                //if (rptLessons != null)
                //{
                //    rptLessons.DataSource = arrList[3];
                //    rptLessons.DataBind();
                //}
                if (rptSessions != null)
                {
                    rptSessions.DataSource = arrList[4];
                    rptSessions.DataBind();
                }
                if (rptTeaOthCourse != null)
                {
                    rptTeaOthCourse.DataSource = arrList[6];
                    rptTeaOthCourse.DataBind();
                }
                if (rptOtherCourse != null)
                {
                    rptOtherCourse.DataSource = arrList[7];
                    rptOtherCourse.DataBind();
                }
                TimeAvaila=arrList[5] as List<Model.WWW_User.User.UserTimeAvailability>;
                //title
                this.DynamicTitle = GetText("{0} - italki {1} Courses", new string[] { CourseInfo.Title, BLF.StaticData.GetLanguageText(CourseInfo.LanguageLearn) });
                this.DynamicKeywords = GetText("{0} , {1} course", new string[] { CourseInfo.Title, BLF.StaticData.GetLanguageText(CourseInfo.LanguageLearn) });
                this.DynamicDescription = Lx.Web.Common.UI.Globals.GetString(CourseInfo.Desc, 150);
            }
            else {
                Response.End();
                return;
            }
        }        
        /// <summary>
        /// 获取课件使用语言文本
        /// </summary>
        /// <param name="value">课件baselanguage</param>
        /// <returns>页面所需文本</returns>
        protected string GetBaseLanText(string value)
        {
            System.Text.StringBuilder strApp = new System.Text.StringBuilder();
            string[] arrStr = value.Split('|');
            int len = arrStr.Length;
            for (int i = 0; i < len; i++)
            {
                strApp.Append(GetText("C2018", new string[] { arrStr[i] }));
                strApp.Append(",");
            }
            return strApp.ToString().Remove(strApp.ToString().Length - 1);
        }
        protected string GetLicense(string value)
        {
            string text = "";
            switch (value)
            {
                case "0":
                    text = GetText("WK628");
                    break;
                case "1":
                    text = GetText("WK622");
                    break;
                case "2":
                    text = GetText("WK623");
                    break;
                case "3":
                    text = GetText("WK624");
                    break;
                case "4":
                    text = GetText("WK625");
                    break;
                case "5":
                    text = GetText("WK626");
                    break;
                case "6":
                    text = GetText("WK627");
                    break;
            }
            return text;
        }
    }
}


